package com.astrob.view;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.astrob.adapters.ViewPager_Specail_ItemAdapter;
import com.astrob.model.SpecialTypeBean;
import com.astrob.nhcmap.R;

import java.util.ArrayList;
import java.util.List;

public class SpecialTypeView extends LinearLayout {
	private Context context;
	/** 保存实体对象链表 */
	private List<SpecialTypeBean> list;
	private ViewPager viewPager;
	private LinearLayout ll_dot;
	private ImageView[] dots;
	/** ViewPager当前页 */
	private int currentIndex;
	/** ViewPager页数 */
	private int viewPager_size;
	/** 默认一页4个item */
	private int pageItemCount = 4;
	private int lineCount = 4; // 每行4个icon

	/** 保存每个页面的GridView视图 */
	private List<View> list_Views;

	public SpecialTypeView(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
		this.list = null;
		initView();
	}

	public SpecialTypeView(Context context) {
		super(context);
		this.context = context;
		this.list = null;
		initView();
	}

	@SuppressWarnings("unchecked")
	public void setData(List<?> list) {
		this.list = (List<SpecialTypeBean>) list;
		initDots();
		setAdapter();
	}

	private void setAdapter() {
		xxx.clear();
		list_Views = new ArrayList<View>();
		for (int i = 0; i < viewPager_size; i++) {
			list_Views.add(getViewPagerItem(i));
		}
		viewPager.setAdapter(new ViewPager_ItemAdapter(list_Views));
	}

	ArrayList<ViewPager_Specail_ItemAdapter> xxx = new ArrayList<ViewPager_Specail_ItemAdapter>();

	public void notifyDataChanged() {
		for (int i = 0; i < xxx.size(); ++i) {
			xxx.get(i).notifyDataSetChanged();
		}
	}

	class ViewPager_ItemAdapter extends PagerAdapter {
		ViewPager_ItemAdapter(List<View> views) {
			mViews = views;
		}

		List<View> mViews;

		@Override
		public int getCount() {
			return mViews.size();
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}

		@Override
		public Object instantiateItem(View arg0, int arg1) {
			((ViewPager) arg0).addView(mViews.get(arg1));
			return mViews.get(arg1);
		}

		@Override
		public void destroyItem(View arg0, int arg1, Object arg2) {
			((ViewPager) arg0).removeView((View) arg2);
		}

	}

	private void initView() {
		View view = LayoutInflater.from(context).inflate(
				R.layout.view_specialtype_vp, null);
		viewPager = (ViewPager) view.findViewById(R.id.ml_vp_type);
		ll_dot = (LinearLayout) view.findViewById(R.id.ml_vp_type_dots);
		addView(view, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
	}

	private int dip2px(float dpValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (dpValue * scale + 0.5f);
	}

	// 初始化底部小圆点
	private void initDots() {
		viewPager_size = (list.size() + pageItemCount - 1) / pageItemCount;

		if (viewPager_size > 0) {
			ll_dot.removeAllViews();
			if (1 == viewPager_size) {
				ll_dot.setVisibility(View.GONE);
			} else if (1 < viewPager_size) {
				ll_dot.setVisibility(View.VISIBLE);
				for (int j = 0; j < viewPager_size; j++) {
					ImageView image = new ImageView(context);
					LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
							dip2px(5), dip2px(5));
					params.setMargins(dip2px(8), 0, dip2px(8), 0);
					image.setBackgroundResource(R.drawable.selector_icon_dot);
					ll_dot.addView(image, params);
				}
			}
		}
		if (viewPager_size != 1) {
			dots = new ImageView[viewPager_size];
			for (int i = 0; i < viewPager_size; i++) {
				dots[i] = (ImageView) ll_dot.getChildAt(i);
				dots[i].setEnabled(true);
				dots[i].setTag(i);
			}
			currentIndex = 0;
			dots[currentIndex].setEnabled(false);
			viewPager
					.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

						@Override
						public void onPageSelected(int arg0) {
							setCurDot(arg0);
						}

						@Override
						public void onPageScrolled(int arg0, float arg1,
								int arg2) {

						}

						@Override
						public void onPageScrollStateChanged(int arg0) {

						}
					});
		}
	}

	/** 当前底部小圆点 */
	private void setCurDot(int positon) {
		if (positon < 0 || positon > viewPager_size - 1
				|| currentIndex == positon) {
			return;
		}
		dots[positon].setEnabled(false);
		dots[currentIndex].setEnabled(true);
		currentIndex = positon;
	}

	private View getViewPagerItem(int index) {
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View layout = inflater.inflate(R.layout.view_specialtype_vp_gv, null);
		GridView gridView = (GridView) layout
				.findViewById(R.id.specialtype_vp_gv);
		gridView.setNumColumns(lineCount);

		ViewPager_Specail_ItemAdapter adapter = new ViewPager_Specail_ItemAdapter(
				context, list, index, pageItemCount);
		xxx.add(adapter);
		gridView.setAdapter(adapter);
		gridView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
									int position, long id) {
				if (null != list.get(position + currentIndex * pageItemCount)
						.getOnClickListener())
					list.get(position + currentIndex * pageItemCount)
							.getOnClickListener().ongvItemClickListener(view);
			}
		});
		return gridView;
	}
}
